<template>
    <div class="register">
        <div class="contain">
            <div class="big-box">
                <div class="big-contain" key="bigContainRegister">
                    <!-- <div class="btitle">创建账户</div> -->
                    <h1>创建账户</h1>
                    <div class="bform">
                        <h3>账号</h3>
                        <el-form-item prop="username" label="" :rules="[
                            {
                                required: true,
                                message: '此项为必填项',
                                trigger: 'blur',
                            },
                        ]">
                            <el-input v-model="form.username" />
                        </el-form-item>
                        <!-- <input type="text" placeholder="用户名" v-model="form.username"> -->
                        <span class="errTips" v-if="existed">* 用户名已经存在！ *</span>
                        <h3>邮箱</h3>
                        <el-form-item prop="useremail" label="" :rules="[
                            {
                                required: true,
                                message: '此项为必填项',
                                trigger: 'blur',
                            },
                        ]">
                            <el-input v-model="form.useremail" />
                        </el-form-item>
                        <h3>密码</h3>
                        <el-form-item prop="userpassword" label="" :rules="[
                            {
                                required: true,
                                message: '此项为必填项',
                                trigger: 'blur',
                            },
                        ]">
                            <el-input type="password" v-model="form.userpassword" />
                        </el-form-item>
                        <!-- <input type="email" placeholder="邮箱" v-model="form.useremail">
                        <input type="password" placeholder="密码" v-model="form.userpassword"> -->
                    </div>
                    <button class="bbutton" @click="register">注册</button>
                </div>
            </div>
            <div class="small-box">
                <div class="small-contain" key="smallContainRegister">
                    <div class="stitle">你好，朋友!</div>
                    <p class="scontent">开始注册，和我们一起学习</p>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import { reactive, toRefs } from 'vue'
export default {
    name: 'register',
    setup() {
        // const store = useStore()
        // const router = useRouter()

        const data = reactive({
            isLogin: false,
            emailError: false,
            passwordError: false,
            existed: false,
            form: {
                username: "",
                useremail: "",
                userpassword: ""
            }
        })
        const changeType = () => {
            this.isLogin = !this.isLogin
            this.form.username = ''
            this.form.useremail = ''
            this.form.userpassword = ''
        }
        const register = () => {
            const self = this;
            if (self.form.username != "" && self.form.useremail != "" && self.form.userpwd != "") {
                self.$axios({
                    method: 'post',
                    url: 'http://127.0.0.1:10520/api/user/add',
                    data: {
                        username: self.form.username,
                        email: self.form.useremail,
                        password: self.form.userpwd
                    }
                })
                    .then(res => {
                        switch (res.data) {
                            case 0:
                                alert("注册成功！");
                                this.login();
                                break;
                            case -1:
                                this.existed = true;
                                break;
                        }
                    })
                    .catch(err => {
                        console.log(err);
                    })
            } else {
                alert("填写不能为空！");
            }
        }
        const handleLogin = () => {
            // 触发跳转
            store.commit('setUserInfo', data.loginData);
            localStorage.setItem("loginData", JSON.stringify(data.loginData))
            // 跳转/user
            router.push({
                path: "/user"
            })
        }
        return {
            ...toRefs(data),
            handleLogin,
            changeType,
            register
        }
    }

}
</script>
<style scoped="scoped">
h1 {
    font-size: 26px;
    color: #70b4e3;
    font-weight: 400;
    padding-bottom: 10px;
}

h3 {
    text-align: left;
    font-size: 16px;
    font-weight: 400;
    color: #70b4e3;
    padding: 10px 0;
}

.register {
    width: 100vw;
    height: 100vh;
    box-sizing: border-box;
    background-image: linear-gradient(to right top, #d16ba5, #c777b9, #ba83ca,
            #aa8fd8, #9a9ae1, #8aa7ec, #79b3f4, #69bff8, #52cffe, #41dfff, #46eefa, #5ffbf1);
}

.contain {
    width: 80%;
    height: 75%;
    position: relative;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    border-radius: 20px;
    overflow: hidden;
    /* box-shadow: 0 0 3px #f0f0f0,
        0 0 6px #f0f0f0; */
    box-shadow: 0 25px 45px rgba(0, 0, 0, 0.3);

}

.big-box {
    width: 70%;
    height: 100%;
    position: absolute;
    top: 0;
    left: 30%;
    transform: translateX(0%);
    transition: all 1s;
}

.big-contain {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}


.bform {
    width: 80%;
    height: 40%;
    padding: 2em 0;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    /* align-items: center; */
}

.bform .errTips {
    display: block;
    width: 50%;
    text-align: left;
    color: red;
    font-size: 0.7em;
    margin-left: 1em;
}

.bform input {
    width: 50%;
    height: 30px;
    border: none;
    outline: none;
    border-radius: 10px;
    padding-left: 2em;
    background-color: #f0f0f0;
}

.bbutton {
    margin-top: 20px;
    width: 20%;
    height: 40px;
    border-radius: 24px;
    border: none;
    outline: none;
    background-image: linear-gradient(120deg, #76daec, 0%, #c5a8de 100%);
    color: #fff;
    font-size: 0.9em;
    cursor: pointer;
}

.small-box {
    width: 30%;
    height: 100%;
    background: linear-gradient(135deg, rgb(44, 134, 142), rgb(55, 154, 124));
    position: absolute;
    top: 0;
    left: 0;
    transform: translateX(0%);
    transition: all 1s;
    border-top-left-radius: inherit;
    border-bottom-left-radius: inherit;
}

.small-contain {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}

.stitle {
    font-size: 1.5em;
    font-weight: bold;
    color: #fff;
}

.scontent {
    font-size: 0.8em;
    color: #fff;
    text-align: center;
    padding: 2em 4em;
    line-height: 1.7em;
}

.sbutton {
    width: 60%;
    height: 40px;
    border-radius: 24px;
    border: 1px solid #fff;
    outline: none;
    background-color: transparent;
    color: #fff;
    font-size: 0.9em;
    cursor: pointer;
}

.big-box.active {
    left: 0;
    transition: all 0.5s;
}

.small-box.active {
    left: 100%;
    border-top-left-radius: 0;
    border-bottom-left-radius: 0;
    border-top-right-radius: inherit;
    border-bottom-right-radius: inherit;
    transform: translateX(-100%);
    transition: all 1s;
}
</style>